package com.itheima.tlias84.aspect;

import com.alibaba.fastjson.JSONObject;
import com.itheima.tlias84.entity.Emp;
import com.itheima.tlias84.mapper.EmpMapper;
import com.itheima.tlias84.mapper.ReportMapper;
import com.itheima.tlias84.po.InsertLogmsgParam;
import com.itheima.tlias84.utils.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * @Aspect标识切面类
 */
@Slf4j
@Aspect
@Component
public class LogAspect {
    //要插入日志 别忘了 插入dao层
    @Resource
    ReportMapper reportMapper;

    @Resource
    HttpServletRequest request;

    @Resource
    EmpMapper empMapper;

    @Around("@annotation(com.itheima.tlias84.annotation.Log)")
    public Object insertLog(ProceedingJoinPoint joinPoint){

        //执行父类方法
        try {
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();
            Object[] args = joinPoint.getArgs();
            Class<?> targetClass = joinPoint.getTarget().getClass();
            String methodName = method.getName();
            // 获取方法参数字符串形式
            String methodParams = Arrays.toString(args);

            long startTime = System.currentTimeMillis();

            // 执行方法
            Object proceed = joinPoint.proceed();

            long endTime = System.currentTimeMillis();
            long costTime = endTime - startTime;
            String token = request.getHeader("token");
            JSONObject jsonObject = JwtUtil.verifyToken(token);

            Emp emp = empMapper.selectEmpByUserName(jsonObject.getString("username"));

            InsertLogmsgParam logMsg = new InsertLogmsgParam();
            logMsg.setOperateEmpId(emp.getId()); // 示例：操作员工ID，这里需要根据实际情况获取
            logMsg.setOperateTime(LocalDateTime.now());
            logMsg.setClassName(targetClass.getName());
            logMsg.setMethodName(methodName);
            logMsg.setMethodParams(methodParams);
            logMsg.setReturnValue(proceed.toString());
            logMsg.setCostTime((int) costTime);
            logMsg.setOperateEmpName(emp.getUsername()); // 示例：操作员工姓名，这里需要根据实际情况获取
            logMsg.setCreateTime(LocalDateTime.now());
            logMsg.setUpdateTime(LocalDateTime.now());
            int result = reportMapper.insertLogmsg(logMsg);
            if (result > 0) {
                log.info("操作 {} 插入日志成功", methodName);
            } else {
                log.error("操作 {} 插入日志失败", methodName);
            }
            // 插入日志


            log.info("操作{}插入日志",joinPoint.getSignature().getName());
            return proceed;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}
